/* Copyright 2012 Nathan Begle
    This file is part of pcoursemanager.

    pcoursemanager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    pcoursemanager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with pcoursemanager.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.*;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionListener;

public class System_Tray {
	TrayIcon icon;
	SystemTray systemTray;
	PopupMenu menu;
	PopupMenu courses_menu;
	MenuItem item_period;
	MenuItem item_exit;
	MenuItem item_addCourse;
	
	public System_Tray(ActionListener al)
	{
		systemTray = SystemTray.getSystemTray();
		Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("Content/pcm_not_available.png"));
		
		menu = new PopupMenu();
		courses_menu = new PopupMenu();
		courses_menu.setLabel("Courses");
		
		item_exit = create_Menu_Item(al, "Exit");
		item_addCourse = create_Menu_Item(al, "Add Course");
		item_period = create_Menu_Item(al, "Set Update Period");
		
		menu.add(courses_menu);
		menu.add(item_addCourse);
		menu.add(item_period);
		menu.add(item_exit);
		
		icon = new TrayIcon(image, "Purdue Course Manager", menu);
		icon.setImageAutoSize(true);
		
		try {
			systemTray.add(icon);
		} catch(AWTException e) {
			//System.out.println("Error adding tray icon!");
		}
	}
	
	
	private MenuItem create_Menu_Item(ActionListener al, String text)
	{
		MenuItem item = new MenuItem();
		
		item.setLabel(text);
		item.setEnabled(true);
		item.setActionCommand(text);
		item.addActionListener(al);
		
		return item;
	}
	
	public void setNotAvailImage()
	{
		Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("Content/pcm_not_available.png"));
		if(!icon.getImage().equals(image))
		{
			icon.setImage(image);
		}
	}
	
	public void setAvailImage()
	{
		Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("Content/pcm_available.png"));
		if(!icon.getImage().equals(image))
		{
			icon.setImage(image);
		}
	}
	
	public void addCourse(MenuItem courseMenuItem)
	{
		courses_menu.add(courseMenuItem);
		//System.out.println("test");
	}
	
	public void removeCourse(MenuItem courseMenuItem)
	{
		courses_menu.remove(courseMenuItem);
	}
	
	public void displayMessage(String title, String message)
	{
		icon.displayMessage(title, message, MessageType.NONE);
	}
}
